package server;

import java.sql.*;

public class jdbc1 {
    public static void main(String[] args) throws Exception {
        update();
        //delete();
        //加载驱动
        // add();

    }

    //增加
    private static void add() throws Exception {
        Connection connection = get();
        //获得执行sql语句的对象
        Statement statement = connection.createStatement();
        //sql语句
        String sql = "insert into student(name ,tid) values(,'信息','1')";
        //执行
        int i = statement.executeUpdate(sql);

        //关联
        connection.close();
        statement.close();
    }

    //删除
    public static void delete() throws Exception {
        Connection connection = get();
        Statement statement = connection.createStatement();
        String sql = "delete from student where id= 1";
        //返回受影响的函数
        int i = statement.executeUpdate(sql);

        statement.close();
        connection.close();
    }

    //改
    public static void update() throws Exception {
        Connection connection = get();
        Statement statement = connection.createStatement();
        String sql = "update student set name= '弄好' where id = 2 ";
        int i = statement.executeUpdate(sql);


        statement.close();
        connection.close();
    }

    //查看
    public static void cha() throws Exception {
        Connection connection = get();
        Statement statement = connection.createStatement();
        String sql = "select * from student where id=1";
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int tid = resultSet.getInt("tid");
            System.out.println(id + "" + name + "" + tid);
        }


        resultSet.close();
        statement.close();
        connection.close();
    }


    public static Connection get() throws SQLException {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (Exception e) {
            e.printStackTrace();
        }
        Connection conn = null;

        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybattis?serverTimezone=UTC", "root", "root");

        return conn;
    }


}
